/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：AgentsDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONObject;

import com.xteam.ecsystem.dao.IAgentsDao;
import com.xteam.ecsystem.entities.BaseAgentInfo;
import com.xteam.ecsystem.entities.BaseDistributeRegion;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 代理商数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class AgentsDaoImpl extends DDLPrefix implements IAgentsDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	/**
	 * @see com.xteam.ecsystem.dao.IAgentsDao#delObject(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public void delObject(String ids) {
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseAgentInfo");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	/**
	 * @see com.xteam.ecsystem.dao.IAgentsDao#getBaiList(int, int, java.lang.String, int)
	 */
	@SuppressWarnings("unchecked")
	public String getBaiList(int pageIndex, int pageSize, String baiName, String baiState) {
		JSONObject jo = new JSONObject();
		
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseAgentInfo.REF + SQL_WHERE +" 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + BaseAgentInfo.REF + SQL_WHERE +" 1=1 ";
			
			if (baiName != null && baiName.length() > 0
					&& !baiName.equals("undefined")
					&& !baiName.equals("null")) {
				hql += SQL_AND + " baiName " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " baiName " + SQL_LIKE + " ? ";
				list.add("%" + baiName + "%");

			}
			
			if(baiState != null && baiState.length() > 0
					&& !baiState.equals("undefined")
					&& !baiState.equals("null")){
				if(!"-1".equals(baiState)){
					hql += SQL_AND + " baiState=? ";
					countHQL += SQL_AND + " baiState=? ";
					list.add(Integer.parseInt(baiState));
				}
			}

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	/**
	 * @see com.xteam.ecsystem.dao.IAgentsDao#saveOrUpdateObject(java.lang.Object, java.lang.String)
	 */
	public void saveOrUpdateObject(Object obj, String id) {
		try {
			if (id != null && !id.equals("") && id.length() > 0){
				this.baseDao.update(obj);
			}else {
				this.baseDao.save(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public String getBaiById(String id) {
		JSONObject jo = new JSONObject();
		
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseAgentInfo.REF + SQL_WHERE +" 1=1 " + SQL_AND + " baiId=?";
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BaseAgentInfo)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	@SuppressWarnings("unchecked")
	public void updateBaiStateById(String id) {
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseAgentInfo");
			
			BaseAgentInfo bai = (BaseAgentInfo)this.baseDao.get(clazz, id);
			if(bai.getBaiState() == 1){
				bai.setBaiState(0);
			}else{
				bai.setBaiState(1);
			}
			
			this.baseDao.saveOrUpdate(bai);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public boolean getBdrBybai(String baiIds){
		boolean isUsed = false;
		List list = new ArrayList();
		String hql = "";
		
		try {
			String oId[] = baiIds.split(",");
			for (String id : oId) {
				hql = SQL_FROM + BaseDistributeRegion.REF + SQL_WHERE + "bdrAgent = ?";
				list = this.baseDao.find(hql, new Object[]{id});
				
				if (list != null && !list.isEmpty()) {
					isUsed = true;
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return isUsed;
	}

}
